#!/usr/bin/env python

from binascii import b2a_hex
from fusion.db.db import DB
from fusion.mai.nava import fetch_oai_dc_record, oai_dc_to_json, atomize
from simplejson import dumps
from urllib2 import randombytes


mediafiles = set([ 
    '318178', '325314', '328906',
    '331176', '335200'
    ])

file_postfix = "-p2p.mp4"
videourl = ('http://old.nava.hu/kg/p2p/%s'+file_postfix)

webhtml ='''
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">     <head id="documenthead">         <link rel="stylesheet" href="blog.css" type="text/css" />         <script type="text/javascript" src="/static/js/prototype.js"></script>         <script type="text/javascript" src="/static/js/httpconnector.js"></script>         <script type="text/javascript" src="/static/js/feed/feedview.js"></script>         <script type="text/javascript" src="/static/js/feed/feedconnector.js"></script>         <script type="text/javascript" src="/static/js/util.js"></script>         <title>P2PFusion Blog</title>     </head>     <body>         <div id="view">         </div>     </body> </html>
'''

webcss ='''
body {      text-align: center;      font-family: sans-serif;  }  #view {     margin: 1em 25%; }  #view hr {     margin: 1em 3%; }  #view i {     display: block;     padding: 1em;     border: thick solid #eee;     margin: 1em 0; }  #view i a {     padding: 0 1em; }  blockquote.comment_content {      font-size: 0.8em;      margin: 0px;      padding-left: 1em;  }  div.comment_author {      font-size: 0.85em;      margin: 0px;  }  div.entry {      padding-bottom: 1.0em;      border-style: solid;      border-color: #000;      border-width: 0 thin;      background-color: #ddd;      padding: 0;  }  div.entry_content {      background-color: #eee;      padding: 1em;      text-align: justify;      }  div#feed_header {      font-size: 3em;      border: thick solid #ddd;      color: #f70;      padding: 0.5em 10%;  }  div#feed_header a {      color: #f70; }  form#add_comment {      font-size:0.8em;  }  input#comment_nickname {      width: 100%;  }  input#comment_tags {      width: 100%;  }  textarea#comment_content {      width: 100%;  }  label#comment_submit {      border: 1px solid black;      padding: 2px;  }  div#entry_tags {      font-size: 0.8em;      text-transform: lowercase;      text-align: right;      font-weight: bold;      background-color: #eee;       }  div.entry_author {      font-size: 0.8em;      text-align: left;     font-style: italic;     padding: 0.2em 2%; }  div.entry_title {      background-color: #07b;      color: #fff;      font-size: 1.2em;      text-align: left;      padding: 0 2%;  }  div.entry_title a {     color: #fff;     font-size: 1.5em; }  div.entry_title a:hover {     background-color: #fff     color: #f70; }   h3 {     background-color: #000;     color: #fff; }  #view table td + td {     padding: 0.2em 0; }  #view table td + td * {     width: 100%; }  #view table td + td *[type="radio"] {     width: auto; }  #view form {     margin: 0; } img { border: none; }
'''

db = DB('~/.fusion/fusion.db')

new = db.create_collection()

db.put(new, "info/title", "NAVA collection")
db.put(new, 'info/type', 'tag:hiit.fi,2007-07:blog')
db.put(new, '/web/index.html', webhtml)
db.put(new, '/web/blog.css', webcss)

for media in mediafiles:
    xml = fetch_oai_dc_record('oai:nava.hu:nava-'+media)
    json = oai_dc_to_json(xml)
    json = atomize(json)

    entry_id = b2a_hex(randombytes(10))

    if not json.has_key('link') or not isinstance(json['link'], list):
        json['link'] = []

    json['link'].append({"rel": "enclosure", "href": "http://localhost/media/"+media+file_postfix})

    db.post(new, '/feed/entries/'+entry_id, dumps(json))

db.close()
